package com.hyc.myvideo.ui.activity.home;

import android.support.v4.app.Fragment;
import android.support.v4.app.FragmentManager;
import android.support.v4.app.FragmentPagerAdapter;
import android.support.v4.view.ViewPager;
import android.view.View;
import android.widget.TextView;

import com.acmenxd.toaster.Toaster;
import com.alibaba.fastjson.JSONObject;
import com.alibaba.fastjson.TypeReference;
import com.flyco.tablayout.SlidingTabLayout;
import com.hyc.myvideo.R;
import com.hyc.myvideo.data.api.model.BaseResult;
import com.hyc.myvideo.data.api.model.CurriculumHour;
import com.hyc.myvideo.ui.base.BaseFActivity;
import com.hyc.myvideo.ui.base.BaseFragment;
import com.hyc.myvideo.ui.fragment.home.CommentFragment;
import com.hyc.myvideo.ui.fragment.home.CourseInfoFragment;

import com.hyc.utils.StringUtils;
import java.util.ArrayList;
import java.util.List;

import butterknife.BindView;
import butterknife.OnClick;

/**
 * 更多简介
 * Created by dfeads on 2017/7/17.
 */

public class AudioMoreActivity extends BaseFActivity {

  /** 标题 */
  @BindView(R.id.activity_audio_title) TextView mTitle;
  /** 练习人数 */
  @BindView(R.id.activity_audio_num) TextView mNum;
  /** 课程简介 */
  @BindView(R.id.activity_audio_more_course_info) TextView mInfo;
  /** 评论 */
  @BindView(R.id.activity_audio_more_comment) TextView mComment;

  @BindView(R.id.activity_audio_more_stl) SlidingTabLayout mTable;
  @BindView(R.id.activity_audio_more_vp) ViewPager mPager;

  private final String[] mTitles = { "课程简介", "评论(255)" };
  private List<BaseFragment> fragments = new ArrayList<>();
  MyPagerAdapter mAdapter;
  private String curriculum_hour_id = "";
  private int type=0;

  private CourseInfoFragment courseInfoFragment = null;
  private CommentFragment commentFragment = null;
  private CurriculumHour curriculumHour;

  @Override public int getContentViewId() {
    return R.layout.activity_audio_more;
  }

  @Override protected void initView() {

    curriculum_hour_id = getIntent().getStringExtra("id");
    type=getIntent().getIntExtra("type",0);
    if (!StringUtils.isEmpty(curriculum_hour_id)) {
      api.getHomeClassHourDetails(curriculum_hour_id);
    }

    courseInfoFragment = new CourseInfoFragment();
    commentFragment = new CommentFragment();
    commentFragment.setCurriculum_hour_id(curriculum_hour_id);
    fragments.add(courseInfoFragment);
    fragments.add(commentFragment);
    mAdapter = new MyPagerAdapter(getSupportFragmentManager());
    mPager.setAdapter(mAdapter);
    mTable.setViewPager(mPager);
    mPager.setCurrentItem(type);
    switch (type){
      case 0:
        mInfo.setTextColor(getResources().getColor(R.color.color_1));
        mComment.setTextColor(getResources().getColor(R.color.color_2));
        break;
      case 1:
        mInfo.setTextColor(getResources().getColor(R.color.color_2));
        mComment.setTextColor(getResources().getColor(R.color.color_1));
        break;
    }


    mPager.addOnPageChangeListener(new ViewPager.OnPageChangeListener() {
      @Override
      public void onPageScrolled(int position, float positionOffset, int positionOffsetPixels) {
      }

      @Override public void onPageSelected(int position) {

        switch (position) {
          case 0:
            mInfo.setTextColor(getResources().getColor(R.color.color_1));
            mComment.setTextColor(getResources().getColor(R.color.color_2));
            break;
          case 1:
            mInfo.setTextColor(getResources().getColor(R.color.color_2));
            mComment.setTextColor(getResources().getColor(R.color.color_1));
            break;
        }
      }

      @Override public void onPageScrollStateChanged(int state) {
      }
    });
  }

  @Override protected void initData() {

  }

  @Override protected void oNext(String resulte, String method) {
    BaseResult<CurriculumHour> curriculums =
        JSONObject.parseObject(resulte, new TypeReference<BaseResult<CurriculumHour>>() {
        });
    if (curriculums.isCode()) {
      curriculumHour = curriculums.getResult();
      setView();
    } else {
      Toaster.show(curriculums.getMsg());
    }
  }

  @Override protected void oError() {

  }

  private void setView() {
    if (curriculumHour == null) {
      return;
    }

    mTitle.setText(curriculumHour.getCurriculum_hour_title());
    mNum.setText(curriculumHour.getCurriculum_people_count() + "人已练习");
    mComment.setText("评论(" + curriculumHour.getHour_comment_count() + ")");
  }

  @OnClick({
      R.id.activity_audio_back, R.id.activity_audio_share, R.id.activity_audio_more_course_info,
      R.id.activity_audio_more_comment
  }) public void onViewClicked(View view) {
    switch (view.getId()) {
      case R.id.activity_audio_back:
        finish();
        break;
      case R.id.activity_audio_share:
        Toaster.show("share");
        break;
      case R.id.activity_audio_more_course_info://课程简介
        mPager.setCurrentItem(0);
        break;
      case R.id.activity_audio_more_comment://评论
        mPager.setCurrentItem(1);
        break;
    }
  }

  private class MyPagerAdapter extends FragmentPagerAdapter {

    public MyPagerAdapter(FragmentManager fm) {
      super(fm);
    }

    @Override public Fragment getItem(int position) {
      return fragments.get(position);
    }

    @Override public int getCount() {
      return mTitles.length;
    }

    @Override public CharSequence getPageTitle(int position) {
      return mTitles[position];
    }
  }
}
